import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class View3 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new View1State();
  }
}

class View1State extends State<View3> {
  ScrollController _scrollController = new ScrollController();
  var _users = [],
  _loading = false;
  void _getData() async{
    setState(() {
      _loading = true;   
    });
    Response response = await Dio().get("http://jsonplaceholder.typicode.com/users");
    setState(() {
      _users.addAll(response.data);
      _loading = false;
    });
  }
  @override
  void initState() {
    super.initState();
    _getData();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        // 触底加载更多
        _getData();
      }
    });
  }
  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
        new Expanded(
          child: ListView.separated(
            itemBuilder: (_, index) {
              return new Card(
                child: new Container(
                  padding: EdgeInsets.fromLTRB(20.0, 60.0, 20.0, 60.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      new Text(index.toString() + 'name:' + _users[index]['name']),
                      new Text('email:' + _users[index]['email']),
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (_, index) {
              // separated构造方法可以定义分割线
              return new Divider();
            },
            itemCount: _users.length,
            controller: _scrollController,
          ),
        ),
        new Center(
          child: _loading ? Container(
            padding: const EdgeInsets.all(16.0),
            alignment: Alignment.center,
            child: SizedBox(
                width: 24.0,
                height: 24.0,
                child: CircularProgressIndicator(strokeWidth: 2.0)
            ),
          ) : null,
        )
      ],
    );
  }
}